package com.example.appointment.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.text.TextUtils;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ViewPagerAdapter extends FragmentStatePagerAdapter {
	private List<String> mDataList = new ArrayList<String>();
	private Map<Integer, Fragment> fragments = new HashMap<Integer, Fragment>();

	public ViewPagerAdapter(FragmentManager fm, List<String> dataList) {
		super(fm);
		mDataList = dataList;
	}
//刷新姐买呢
	@Override
	public int getItemPosition(Object object) {
		return super.getItemPosition(object);
	}



	@Override
	public Fragment getItem(int position) {
		if (fragments != null && fragments.containsKey(position)) {
			return fragments.get(position);
		}

		String name = mDataList.get(position);
		if (!TextUtils.isEmpty(name)) {
			try {
				Fragment fragment = (Fragment) Class.forName(name)
						.newInstance();
				fragments.put(position, fragment);
				return fragment;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public int getCount() {
		return mDataList.size();
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		super.destroyItem(container, position, object);
		// fragments.remove(position);
	}

	// @Override
	// public Object instantiateItem(ViewGroup arg0, int arg1) {
	// if(fragments != null && fragments.containsKey(arg1)){
	// return fragments.get(arg1);
	// }
	// Object object = super.instantiateItem(arg0, arg1);
	// fragments.put(arg1, (Fragment)object);
	// // fragments.add((Fragment) object);
	// return object;
	// }

	public Fragment getFragment(int position) {
		return fragments.get(position);
	}

}
